import { createRouter,createWebHashHistory } from "vue-router";

const routes = [
    {
        path:'/',
        component: () => import('../views/Index.vue'),
    },
    {
        path:'/index',
        component: () => import('../views/Index.vue'),
    },
    {
        path:'/article',
        component: () => import('../views/Article.vue'),
    },
    {
        path:'/home',
        component: () => import('../views/Home.vue'),
    },
    {
        path:'/login',
        component:() => import('../views/Login.vue'),
        //增加是否已经登录的判断逻辑
        beforeEnter(to,from,next){
            const isLogin = sessionStorage.getItem('isLogin');
            isLogin ? next({path:'/index'}):next()
        }
    },
    {
        path:'/word2study',
        component:() => import('../views/WordStudy.vue')
    },
    {
        path:'/word2review',
        component:() => import('../views/WordReview.vue')
    },
    {
        path:'/wordlist',
        component:() => import('../views/WordList.vue')
    },
    {
        path:'/congratulation',
        component:() => import('../views/Congratulation.vue'),
        beforeEnter(to,from,next){
            if(from.path === '/index'){
                sessionStorage.setItem('backTime','1');
            }else{
                sessionStorage.setItem('backTime','2');
            }
            console.log('saved')
            next();
        }
    },
]

const router = createRouter({
    history:createWebHashHistory(),
    routes
})
router.beforeEach((to, from, next) => {
    const hasToken = localStorage.getItem('token');
    // console.log(isLogin);
    if(hasToken || to.path === '/login'){
        next()
    }else{
        next({path:'/login'});
    }
    
  })
export default router